/**
 * Created by superman on 17/2/16.
 */

import Vue from 'vue'
import VueRouter from 'vue-router'
import router_admin from './router_admin'
import router_game from './router_game'
import router_game_admin from './router_game_admin'
import store from '@/store/store'
import * as types from '@/store/types'
import VueCookies from 'vue-cookies'

const isMobile = () => {
    return /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i.test(
    navigator.userAgent
    );
};
const pathApp = isMobile() ? 'app/' : '';

Vue.use(VueRouter)
// 
const routes = [
    {
        path: '/admin',
        meta: {
            requireAuth: true
        },
        component: () => import('@/page/admin/index.vue'),
        children: router_admin
    },
    {
        path: '/admin/login',
        component: () => import('@/page/admin/login.vue'),
        meta: {
            title: '系统管理中心'
        }
    },
    {
        path: '/',
        component:()=>import(`@/page/game/zhuxian/${pathApp}index.vue`),
        children: router_game,
        meta: { title: '东方诛仙，易宝天行，在线摆摊系统'},
        // component: (resolve) => require([`@/page/game/zhuxian/${path}index.vue`], resolve)
    },
    {
        path: '/admin/zx/index',
        component:()=>import(`@/page/game/zhuxian/admin/index.vue`),
        children: router_game_admin,
        meta: { title: '后台管理，东方诛仙，易宝天行，在线摆摊系统'},
        // component: (resolve) => require([`@/page/game/zhuxian/${path}index.vue`], resolve)
    },
];

const router = new VueRouter({
    // mode: 'history',
    // base: __dirname,
    routes,
});

if (VueCookies.isKey('token')) {
    // console.log(JSON.parse(window.localStorage.getItem('power')))
    let data = {
        token: VueCookies.get('token'),
        user: JSON.parse(window.localStorage.getItem('user')),
        power: JSON.parse(window.localStorage.getItem('power')),
    };
    
    store.commit(types.LOGIN, data)
}

router.beforeEach((to, from, next) => {
    //设置title
    if (to.meta && to.meta.title)
        window.document.title = to.meta.title;
    else
        window.document.title = '东方诛仙，易宝天行，在线摆摊系统';

    next();
    // //  判断该路径是否需要登录权限
    // if (to.matched.some(r => r.meta.requireAuth)) {
    //     if (store.state.token&&store.state.power) {
    //         //  判断该路径是否拥有权限
    //         next();
    //         // if (CheckPowerByUrl(to.path)) {
    //         //     next();
    //         // }
    //         // else
    //         //     next(new Error('该页面没有访问权限'));
    //     }
    //     else {
    //         next(new Error('该页面没有访问权限'));
    //         next({
    //             path: '/',
    //             query: { redirect: to.fullPath }
    //         })
    //     }
    // }
    // else {
    //     next();
    // }
})

//检查url链接是否有权限
// function CheckPowerByUrl(path) {
//     if(localStorage.power){
//         var listMenu = JSON.parse(localStorage.power).listMenu;
//         for (let i = 0; i < listMenu.length; i++) {
//             let itemMenu = listMenu[i];
//             //判断该路径在权限中是否存在，如果存在，则返回正确
//             if (itemMenu.Url == path)
//                 return true;
//         }
//         return true;
//     }
// }


const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


router.onError(function (error) {
    alert(error.message)
})
export default router;